// Copyright Wilfried Woivre - http://blog.woivre.fr

using System;
using System.Collections.Generic;
using System.Data.Services.Providers;
using System.Linq;

namespace Demo.TableStorageDataServices.Web.Services
{
	public class DataSourceQueryProvider<T> : IDataServiceQueryProvider where T : class, IDataSource
	{
        private readonly IDataServiceMetadataProvider _metadataProvider;
        private T _currentDataSource; 

        public object CurrentDataSource 
        { 
            get { 
                return _currentDataSource; 
            } 
            set { 
                _currentDataSource = value as T; 
            } 
        }  

        public DataSourceQueryProvider(IDataServiceMetadataProvider metadataProvider)
        {
            _metadataProvider = metadataProvider;
        }

        public object GetOpenPropertyValue(object target, string propertyName)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<KeyValuePair<string, object>> GetOpenPropertyValues(object target)
        {
            throw new NotImplementedException();
        }

        public object GetPropertyValue(object target, ResourceProperty resourceProperty)
        {
            throw new NotImplementedException();
        }

        public IQueryable GetQueryRootForResourceSet(ResourceSet resourceSet)
        {
            return _currentDataSource.GetQueryable(resourceSet);
        }

        public ResourceType GetResourceType(object target)
        {
            Type type = target.GetType();
            return _metadataProvider.Types
               .Single(t => t.InstanceType == type); 
        }

        public object InvokeServiceOperation(ServiceOperation serviceOperation, object[] parameters)
        {
            throw new NotImplementedException();
        }

        public bool IsNullPropagationRequired
        {
            get { return true; }
        }
    }
}

